package yxy.game.pm2.command.kow;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Format;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.KowMessage;
import yxy.game.pm2.module.hero.FormatHandle;
import yxy.game.pm2.module.kow.KowHandle;

/** 天下第一 天下第一小组赛挑战 */
@MessageType(proto = Command.KowGroupChanllenge, classtype = KowMessage.KowGroupChanllengeRequest.class)
public class CMD_KowGroupChanllenge extends Command<KowMessage.KowGroupChanllengeRequest> {

	@Override
	public void execute(MessageSource messageSource, KowMessage.KowGroupChanllengeRequest request) {
		String playerCode = messageSource.getTag();
		String oppoCode = request.getCode();

		KowMessage.KowGroupChanllengeResponse.Builder response = KowMessage.KowGroupChanllengeResponse.newBuilder();

		FormatHandle formatHandle = new FormatHandle();
		Result result = formatHandle.check(playerCode, formatHandle.get(playerCode, Format.FormatType.普通));
		if (!result.equals(Result.Success)) {
			send(messageSource, response, result);   //  重复武将判断
			return;
		}

		try {
			 result = new KowHandle().battleResponseExecute(playerCode, oppoCode, response);
			send(messageSource, response, result);
		} catch (Exception e) {
			e.printStackTrace();
			send(messageSource, response, Result.DataError);
		}
	}

	private void send(MessageSource messageSource, KowMessage.KowGroupChanllengeResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}